package com.ln.store.dao.Impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.ln.store.dao.UserDao;
import com.ln.store.domain.User;
import com.ln.store.utils.JDBCUtils;

public class UserDaoImpl implements UserDao {

	public void userRegister(User user) throws SQLException {
	
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		
		String sql = "insert into user value(?,?,?,?,?,?,?,?,?,?)";
		
		Object[] param = {user.getUid(),user.getUsername(),user.getPassword(),user.getName(),user.getEmail(),user.getTelephone(),user.getBirthday(),user.getSex(),user.getState(),user.getCode()};
		
		queryRunner.update(sql, param);
	}

	public User userActive(String code) throws SQLException {
		
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		
		String sql = "select * from user where code=?";
		
		return queryRunner.query(sql, new BeanHandler<User>(User.class),code);
	}

	public void updateUser(User user) throws SQLException {
		
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		
		String sql = "update user set username=?,password=?,name=?,email=?,telephone=?,birthday=?,sex=?,state=?,code=? where uid=?";
		
		Object [] param = {user.getUsername(),user.getPassword(),user.getName(),user.getEmail(),user.getTelephone(),user.getBirthday(),user.getSex(),user.getState(),user.getCode(),user.getUid()};
		
		queryRunner.update(sql, param);
		
	}

	public User userLogin(User user)  throws SQLException{
		
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		
		String sql = "select * from user where username=? and password=?";
		
		return queryRunner.query(sql, new BeanHandler<User>(User.class),user.getUsername(),user.getPassword());
	}

}
